//	Pixlib
import com.bourre.data.collections.Map;
import com.bourre.log.Logger;
import com.bourre.log.LogLevel;

//	Oregano
import org.omus.data.PropertySet;
import org.omus.util._Error;

//	Pixlib API for Oregano
import net.webbymx.pixgano.handlers.data.IPropertySetHandler;
import net.webbymx.pixgano.handlers.data.BasicPropertySetHandler;
import net.webbymx.pixgano.events.PropertySetEvent;

class net.webbymx.pixgano.handlers.data.PropertySetHandlerManager {
/* ****************************************************************************
* PRIVATE STATIC VARIABLES
**************************************************************************** */
	private static var _oI			: PropertySetHandlerManager;	


/* ****************************************************************************
* PUBLIC STATIC VARIABLES
**************************************************************************** */		
	public static var EVENT_ON_SYNCHRONIZE		: PropertySetEvent =  new PropertySetEvent( "onSynchronize" );
	public static var EVENT_ON_LOAD				: PropertySetEvent = new PropertySetEvent( "onLoad" );

	
/* ****************************************************************************
* PRIVATE VARIABLES
**************************************************************************** */
	private var _mPS				: Map;	
	private var _mL					: Map;	

/* ****************************************************************************
* CONSTRUCTOR
**************************************************************************** */
	function PropertySetHandlerManager() {
		_mPS = new Map();
		_mL = new Map();
	}
	
/**
* Singleton access
*/	
	public static function getInstance() : PropertySetHandlerManager {
		if ( !_oI ) _oI = new PropertySetHandlerManager();
		return _oI; 
	}
	
/* ****************************************************************************
* PRIVATE VARIABLES
**************************************************************************** */
	private function _recordEventListener( s : String ) : Void {
		if ( _mL.containsKey( s ) ) {
			var args : Array = _mL.get( s );
			BasicPropertySetHandler( _mPS.get( s ) ).addEventListener.apply( BasicPropertySetHandler( _mPS.get( s ) ), args );
		}
	}
	
/* ****************************************************************************
* PUBLIC FUNCTIONS
**************************************************************************** */
/**
* create a PropertySet Handler and map it with the name given as argument
* @param	s
* @param	ps
*/
	public function createPropertySetHandler( s : String, ps : PropertySet ) : Void {
		if ( _mPS.containsKey( s ) ) {
			var bph : BasicPropertySetHandler = new BasicPropertySetHandler( ps );
			bph.setEventListeners( BasicPropertySetHandler( _mPS.get( s ) ).getEventListeners() );
			_mPS.remove( s );
			_mPS.put( bph );
		} else {
			_mPS.put( s, new BasicPropertySetHandler( ps ) );
			_recordEventListener( s );
		}
	}
	
/**
* This function is called only when you have created ( by extending the BasicPropertySetHandler )
* a custom PropertySetHandler
* @param	s
* @param	psh
*/
	public function registerPropertySetHandler( s : String, psh : BasicPropertySetHandler ) : Void {
		if ( _mPS.containsKey( s ) ) {
			psh.setEventListeners( BasicPropertySetHandler( _mPS.get( s ) ).getEventListeners() );
			_mPS.remove( s );
			_mPS.put( psh );
		} else {
			_mPS.put( s, psh );
			_recordEventListener( s );
		}
	}
	
	public function registerEventListener( s : String, event: PropertySetEvent, o, func: String, prop: Array ) : Void {
		var a : Array = arguments.slice( 1 );
		if ( _mPS.containsKey( s ) ) {
			BasicPropertySetHandler( _mPS.get( s ) ).addEventListener( event, o, func, prop )
		} else {
			_mL.put( s, a );
		}
	}
	
	
/**
* Delete the PropertySetHandler
* @param	s
*/
	public function deletePropertySetHandler( s : String ) : Boolean {
		if ( _mPS.containsKey( s ) ) {
			BasicPropertySetHandler( _mPS.get( s ) ).removeAllEventListeners();
			BasicPropertySetHandler( _mPS.get( s ) ).unlink();
			_mPS.remove( s );
			return true;
		}
		return false;
	}
	
	public function getPropertySetHandler( s : String ) : BasicPropertySetHandler {
		return _mPS.get( s );
	}
}
